package jmine.tec.persist.schema.impl.parsers;

import java.util.regex.Matcher;

import jmine.tec.persist.schema.impl.elements.Schema;
import jmine.tec.persist.schema.impl.elements.SequenceElement;

/**
 * Parser do statement de criação de sequence.
 * 
 * @author lundberg
 */
public class SequenceParser extends AbstractSchemaElementParser<SequenceElement> {

    private static final int SEQUENCE_NAME_GROUP_INDEX = 1;

    private static final int INITIAL_VALUE_GROUP_INDEX = 2;

    /**
     * Construtor
     * @param schema schema
     */
    public SequenceParser(Schema schema) {
        super("create\\s+sequence\\s+(" + TableParser.TABLE_NAME_REGEX  + ")(?:\\s+start\\s+with\\s+(\\d+))?", schema);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SequenceElement doParse(Matcher matcher) {
        String name = matcher.group(SEQUENCE_NAME_GROUP_INDEX);
        SequenceElement sequence = new SequenceElement(name, getSchema());
        if (matcher.group(INITIAL_VALUE_GROUP_INDEX) != null) {
            sequence.setInitialValue(Integer.parseInt(matcher.group(INITIAL_VALUE_GROUP_INDEX)));
        }
        return sequence;
    }

}
